package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetMeanServiceImpl implements SetMealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;
    /*
    * 新增套餐
    * */
    @Transactional
    @Override
    public void insert(SetmealDTO setmealDTO) {
        //插入套餐信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);
        //插入套餐菜品信息
        List<SetmealDish> dishes = setmealDTO.getSetmealDishes();
        if(dishes != null && dishes.size() > 0) {
            for (SetmealDish setmealDish : dishes) {
                setmealDish.setSetmealId(setmeal.getId());
            }
            //批量插入
            setMealDishMapper.insertBatch(dishes);
        }
    }

    /*
    * 套餐分页查询
    * */

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //配置分页参数
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        //分页查询
        Page<Setmeal> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        //结果封装
        return new PageResult(page.getTotal(), page.getResult());
    }

    /*
    * 套餐批量删除
    * */

    @Override
    public void deleteBatch(List<Long> ids) {
        //检查套餐状态，如果在售，无法删除
        for(Long id : ids) {
            Setmeal setmeal = setmealMapper.selectById(id);
            if(setmeal.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException("套餐在售，无法删除");
            }
        }
        //删除套餐
        setmealMapper.deleteBatch(ids);
        //删除关联的菜品
        setMealDishMapper.deleteBatch(ids);
    }


    /*
    * 根据id查询套餐
    * */

    @Override
    public SetmealDTO selectById(Long id) {
        //查询套餐
        SetmealDTO setmealDTO = new SetmealDTO();
        Setmeal setmeal = setmealMapper.selectById(id);
        BeanUtils.copyProperties(setmeal,setmealDTO);
        //查询关联的菜品
        List<SetmealDish> setmealDishes = setMealDishMapper.selectBySetmealId(id);
        setmealDTO.setSetmealDishes(setmealDishes);
        return setmealDTO;
    }

    /*
    * 修改套餐
    * */
    @Transactional
    @Override
    public void update(SetmealDTO setmealDTO) {
        //更新套餐信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        //删除原有的套餐菜品信息
        setMealDishMapper.deleteBySetmealId(setmealDTO.getId());
        //插入新的套餐菜品信息
        List<SetmealDish> dishes = setmealDTO.getSetmealDishes();
        if(dishes != null && dishes.size() > 0) {
            for (SetmealDish setmealDish : dishes) {
                setmealDish.setSetmealId(setmeal.getId());
            }
            //批量插入
            setMealDishMapper.insertBatch(dishes);
        }
    }

    /*
    * 修改套餐状态起售或停售
    * */

    @Override
    public void updateStatus(Integer status, Long id) {
        Setmeal setmeal = setmealMapper.selectById(id);
        setmeal.setStatus(status);
        setmealMapper.update(setmeal);
    }
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

}
